<?php

/**
 * This is the model class for table "grain".
 *
 * The followings are the available columns in table 'grain':
 * @property integer $id
 * @property string $nom
 * @property string $descr
 * @property integer $valeur
 * @property string $unite
 * @property string $status
 * @property integer $iddomaine
 * @property integer $idtype
 * @property integer $idseliste
 *
 * The followings are the available model relations:
 * @property Demande[] $demandes
 * @property Seliste[] $selistes
 * @property Domaine $iddomaine0
 * @property Type $idtype0
 */
class Grain extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'grain';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nom, iddomaine, idtype, status', 'required'),
			array('valeur, iddomaine, idtype, idseliste, status', 'numerical', 'integerOnly'=>true),
			array('nom', 'length', 'max'=>255),
			array('unite', 'length', 'max'=>1),
			array('descr', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nom, descr, valeur, unite, status, iddomaine, idtype, idseliste', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'demandes' => array(self::HAS_MANY, 'Demande', 'idgrain'),
			'selistes' => array(self::MANY_MANY, 'Seliste', 'propose(idgrain, idseliste)'),
			'iddomaine0' => array(self::BELONGS_TO, 'Domaine', 'iddomaine'),
			'idtype0' => array(self::BELONGS_TO, 'Type', 'idtype'),
			'idseliste0' => array(self::BELONGS_TO, 'Seliste', 'idseliste'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Numéro',
			'nom' => 'Nom',
			'descr' => 'Description',
			'valeur' => 'Quantité pour un tuyau',
			'unite' => 'Unité',
			'status' => 'Status',
			'iddomaine' => 'Domaine',
			'idtype' => 'Type',
			'idseliste' => 'Seliste',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.nom',$this->nom,true);
		$criteria->compare('t.status',$this->status);
		if ($this->idtype>0) $criteria->compare('t.idtype',$this->idtype,true);
		if ($this->iddomaine>0) $criteria->compare('t.iddomaine',$this->iddomaine,true);
		if ($this->idseliste>0) $criteria->compare('t.idseliste',$this->idseliste,true);
		$criteria->with = array('idtype0','iddomaine0','idseliste0');
		
		$sort = new CSort();
		$sort->attributes = array(
			'id',
			'nom',
			'descr',
			'sseliste'=>array(
				'asc'=>'idseliste0.pseudo',
				'desc'=>'idseliste0.pseudo desc',
				'label'=>'Ajouté par'
			),
			'sdomaine'=>array(
				'asc'=>'iddomaine0.lib',
				'desc'=>'iddomaine0.lib desc',
				'label'=>'Domaine'
			),
			'stype'=>array(
				'asc'=>'idtype0.lib',
				'desc'=>'idtype0.lib desc',
				'label'=>'Type'
			),
			'sstatus'=>array(
					'asc'=>'status',
					'desc'=>'status desc',
					'label'=>'Status'
			),
		);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>$sort,
			'pagination'=>array(
					'pageSize'=>40,
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Grain the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
